package asia.lxq.cic.service;

import asia.lxq.cic.aop.NeedAop;
import asia.lxq.cic.config.GlobalConfig;
import asia.lxq.cic.dao.AppealMapper;
import asia.lxq.cic.dao.AreaMapper;
import asia.lxq.cic.dao.GroupMapper;
import asia.lxq.cic.dao.UserMapper;
import asia.lxq.cic.entity.Appeal;
import asia.lxq.cic.entity.Result;
import asia.lxq.cic.entity.User;
import asia.lxq.cic.util.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AppealService {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    AppealMapper appealMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    AreaMapper areaMapper;
    @Autowired
    GroupMapper groupMapper;

    public Result<Object> getAppealList(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User user= (User) redisUtil.get(GlobalConfig.PRE_USERINFO_REDIS+authentication.getPrincipal());
        QueryWrapper<Appeal> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("appeal_belong_id",user.getCollegeId());
        List<Appeal> appealList = appealMapper.selectList(queryWrapper);
        appealList.forEach(appeal ->{
            appeal.query(userMapper);
            appeal.getAppealUser().query(areaMapper);
            appeal.setAppealObjName(groupMapper.selectById(appeal.getAppealObjId()).getGroupName());
        });
        return Result.success(appealList);
    }

    //todo 验证该申请是否早就已经受理，如果在此之前就受理，就拒绝修改；此接口只能由管理员调用
    @NeedAop
    public Result<Object> scopeAppeal(Appeal appeal){
        Appeal verifyAppeal = appealMapper.selectById(appeal.getAppealId());
        if (!verifyAppeal.getAppealStatus().equals("正在受理"))return Result.error("该请求已经受理，不可再次更改");
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        User adminUser= (User) redisUtil.get(GlobalConfig.PRE_USERINFO_REDIS+authentication.getPrincipal());
        User user = userMapper.selectById(appeal.getAppealUserId());
        String[] split = user.getBelong().split(",");
        if (appeal.getAppealStatus().equals("同意")){
            split=adminUser.getBelong().split(",");
            split[3]=appeal.getAppealObjId()+"";
        }else split[3]="0";
        user.setBelong(String.format("%s,%s,%s,%s",split[0],split[1],split[2],split[3]));
        appealMapper.updateById(appeal);
        userMapper.updateById(user);
        return Result.success(user);
    }

    @NeedAop
    public Result<Object> addAppeal(Appeal appeal,Authentication authentication){
        User user= (User) redisUtil.get(GlobalConfig.PRE_USERINFO_REDIS+authentication.getPrincipal());
        appeal.setAppealUserId(user.getUserId());
//        appeal.setAppealBelongId(user.getCollegeId());
        appealMapper.insert(appeal);
        String[] split = user.getBelong().split(",");
        split[3]="-1";
        user.setBelong(String.format("%s,%s,%s,%s",split[0],split[1],split[2],split[3]));
        userMapper.updateById(user);
        return Result.success(user);
    }
}
